//
//  GQHQutationDetailView.m
//  Xanthium
//
//  Created by GuanQinghao on 12/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHQutationDetailView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHQutationDetailView () <UITableViewDelegate,UITableViewDataSource,GQHQuotationDetailCellDelegate>

@end


@implementation GQHQutationDetailView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight + 145);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _qh_qutationModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 135.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"aCell";
    GQHQuotationDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHQuotationDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.qh_delegate = self;
    cell.qh_dialButton.tag = indexPath.row;
    cell.qh_contractButton.tag = indexPath.row;
    
    GQHQutationModel *qutationModel = _qh_qutationModelArray[indexPath.row];
    if ([qutationModel.qh_quoteState isEqualToString:@"01"]) {
        /// 已报价状态
        [cell.qh_contractButton setTitle:@"发送协议" forState:UIControlStateNormal];
    }
    if ([qutationModel.qh_quoteState isEqualToString:@"02"]){
        /// 已中标状态
        [cell.qh_contractButton setTitle:@"撤回协议" forState:UIControlStateNormal];
    }
    if ([qutationModel.qh_quoteState isEqualToString:@"03"]) {
        /// 已承运状态
        [cell.qh_contractButton setTitle:@"查看协议" forState:UIControlStateNormal];
    }
    
    /// 头像
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:qutationModel.qh_quoterAvatar];
    NSURL *URL = [NSURL URLWithString:URLString];
    [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    cell.qh_nameLabel.text = qutationModel.qh_quoterName;
    cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%@/%@吨",qutationModel.qh_normalLength,qutationModel.qh_model,qutationModel.qh_normalLoading];
    cell.qh_quoteLabel.text = [NSString stringWithFormat:@"报价：%@ 元/吨",qutationModel.qh_amountString];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)qh_createOrCheckOrCancelContract:(UIButton *)sender {
    DLog(@"发送或查看或取消协议");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_createOrCheckOrCancelContract:)]) {
        
        [_qh_delegate qh_createOrCheckOrCancelContract:sender];
    }
}

- (void)qh_dialMobile:(UIButton *)sender {
    DLog(@"联系司机");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_dialMobile:)]) {
        
        [_qh_delegate qh_dialMobile:sender];
    }
}

@end
